import wx


class MainWindow(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title='计算器', pos=(100, 100), size=(250, 400))
        self.pl = wx.Panel(self)
        self.entry = wx.TextCtrl(self.pl, pos=(10, 10), size=(215, 50), style=wx.TE_RIGHT)
        # 第一行
        wx.Button(self.pl, label='C', pos=(10, 80), size=(50, 50)).Bind(wx.EVT_BUTTON, self.click_clear)
        wx.Button(self.pl, label='/', pos=(65, 80), size=(50, 50)).Bind(wx.EVT_BUTTON, self.click_div)
        wx.Button(self.pl, label='*', pos=(120, 80), size=(50, 50)).Bind(wx.EVT_BUTTON, self.click_mul)
        wx.Button(self.pl, label='<-', pos=(175, 80), size=(50, 50)).Bind(wx.EVT_BUTTON, self.click_back)
        # 第二行
        wx.Button(self.pl, label='7', pos=(10, 135), size=(50, 50)).Bind(wx.EVT_BUTTON, self.click_7)
        wx.Button(self.pl, label='8', pos=(65, 135), size=(50, 50)).Bind(wx.EVT_BUTTON, self.click_8)
        wx.Button(self.pl, label='9', pos=(120, 135), size=(50, 50)).Bind(wx.EVT_BUTTON, self.click_9)
        wx.Button(self.pl, label='-', pos=(175, 135), size=(50, 50)).Bind(wx.EVT_BUTTON, self.click_sub)
        # 第三行
        wx.Button(self.pl, label='4', pos=(10, 190), size=(50, 50)).Bind(wx.EVT_BUTTON, self.click_4)
        wx.Button(self.pl, label='5', pos=(65, 190), size=(50, 50)).Bind(wx.EVT_BUTTON, self.click_5)
        wx.Button(self.pl, label='6', pos=(120, 190), size=(50, 50)).Bind(wx.EVT_BUTTON, self.click_6)
        wx.Button(self.pl, label='+', pos=(175, 190), size=(50, 50)).Bind(wx.EVT_BUTTON, self.click_add)
        # 第四行
        wx.Button(self.pl, label='1', pos=(10, 245), size=(50, 50)).Bind(wx.EVT_BUTTON, self.click_1)
        wx.Button(self.pl, label='2', pos=(65, 245), size=(50, 50)).Bind(wx.EVT_BUTTON, self.click_2)
        wx.Button(self.pl, label='3', pos=(120, 245), size=(50, 50)).Bind(wx.EVT_BUTTON, self.click_3)
        wx.Button(self.pl, label='=', pos=(175, 245), size=(50, 105)).Bind(wx.EVT_BUTTON, self.click_eq)
        # 第五行
        wx.Button(self.pl, label='0', pos=(10, 300), size=(105, 50)).Bind(wx.EVT_BUTTON, self.click_0)
        wx.Button(self.pl, label='.', pos=(120, 300), size=(50, 50)).Bind(wx.EVT_BUTTON, self.click_dot)

    def click_clear(self, event):
        self.entry.Clear()

    def click_div(self, event):
        self.entry.AppendText('/')

    def click_mul(self, event):
        self.entry.AppendText('*')

    def click_back(self, event):
        self.entry.Remove(self.entry.GetLastPosition() - 1, self.entry.GetLastPosition())

    def click_7(self, event):
        self.entry.AppendText('7')

    def click_8(self, event):
        self.entry.AppendText('8')

    def click_9(self, event):
        self.entry.AppendText('9')

    def click_sub(self, event):
        self.entry.AppendText('-')

    def click_4(self, event):
        self.entry.AppendText('4')

    def click_5(self, event):
        self.entry.AppendText('5')

    def click_6(self, event):
        self.entry.AppendText('6')

    def click_add(self, event):
        self.entry.AppendText('+')

    def click_1(self, event):
        self.entry.AppendText('1')

    def click_2(self, event):
        self.entry.AppendText('2')

    def click_3(self, event):
        self.entry.AppendText('3')

    def click_eq(self, event):
        self.entry.SetValue(str(eval(self.entry.GetValue())))

    def click_0(self, event):
        self.entry.AppendText('0')

    def click_dot(self, event):
        self.entry.AppendText('.')


if __name__ == '__main__':
    app = wx.App()
    # 创建窗口
    frm = MainWindow()
    # 显示窗口
    frm.Show()
    app.MainLoop()
